import { HaoEncrypt } from "./HaoEncrypt";
import { Logger } from "./Logger";

export class LocalStorage {

    public static GamePreFlag: string = "JHaoGm-";

    public static setItem(key: string, value: string): void {
        let saveStr: string = HaoEncrypt.encode(value);
        localStorage.setItem(LocalStorage.GamePreFlag + key, saveStr);
    }

    public static getItem(key: string): string {
        let str: string = localStorage.getItem(LocalStorage.GamePreFlag + key) || "";
        let result: string = str;
        if (str) {
            result = HaoEncrypt.decode(str);
        }
        return result
    }

    public static removeItem(key: string): void {
        localStorage.removeItem(LocalStorage.GamePreFlag + key);
    }

    public static getInt(key: string): number {
        let tempValue: string = LocalStorage.getItem(key);
        let result: number = 0;
        if (tempValue) {
            result = parseInt(tempValue);
        }
        return result;
    }

    public static setInt(key: string, value: number): void {
        LocalStorage.setItem(key, value.toString());
    }

    public static getFloat(key: string): number {
        let tempValue: string = LocalStorage.getItem(key);
        let result: number = 0;
        if (tempValue) {
            result = parseFloat(tempValue);
        }
        return result;
    }

    public static setFloat(key: string, value: number): void {
        LocalStorage.setItem(key, value.toString());
    }

    public static getBoolean(key: string): boolean {
        let temp: number = LocalStorage.getInt(key);
        if (temp == 1) {
            return true;
        }
        return false;
    }

    public static setBoolean(key: string, value: boolean) {
        if (value) {
            LocalStorage.setInt(key, 1);
        } else {
            LocalStorage.setInt(key, 0);
        }
    }

    public static clear() {
        localStorage.clear();
    }
}